/**
 * This software is distributed under the terms of the MIT License.
 * Copyright (c) 2020 CrobTech.
 * Author: Jason Zhu
 * Contributors: https://gitee.com/JasonZhu2021/tmf8801_tof_demo
 */

#ifndef TMF8801_GENERIC_H_
#define TMF8801_GENERIC_H_

/**************************************************************************************
 * INCLUDE
 **************************************************************************************/
#include "stddef.h"
#include "TMF8801_Reg.h"

/**************************************************************************************
 * MCRO DEFINE
 **************************************************************************************/
#define TMF8801_OK              0
#define TMF8801_ERROR          -1

/**************************************************************************************
 * CONSTANTS
 **************************************************************************************/
enum{
  FALSE = 0,
  TRUE,
};
typedef enum{
  None            =  0,
  Timeout         = -1,
  ChipId          = -2,
}TMF8801_Error;
/**************************************************************************************
 * TYPE DEFINE
 **************************************************************************************/
typedef int32_t (*TMF8801_Init_Func)(void);
typedef int32_t (*TMF8801_WriteReg_Func)(uint16_t, uint16_t, uint8_t *, uint16_t);
typedef int32_t (*TMF8801_ReadReg_Func)(uint16_t, uint16_t, uint8_t *, uint16_t);

typedef struct 
{
  TMF8801_Init_Func         Init;
  uint8_t                   Address;
  TMF8801_WriteReg_Func     WriteReg;
  TMF8801_ReadReg_Func      ReadReg;
}TMF8801_IO_t;

typedef union 
{
  struct
  {
    uint8_t  result_number;
    uint8_t  result_info;
    uint16_t distance_peak_0_mm;
  } field;
  uint8_t buf[4];
}ObjectDetectionData;

typedef struct 
{
  TMF8801_IO_t              IO;
  tmf8801_ctx_t             Ctx;      
  TMF8801_Error             Err;        
}TMF8801_Object_t;
/**************************************************************************************
 * FUNC DEFINE
 **************************************************************************************/
int32_t TMF8801_RegisterBusIO(TMF8801_Object_t *pObj, TMF8801_IO_t *pIO);
int32_t TMF8801_Init(TMF8801_Object_t *pObj, uint8_t *_calib_data, uint8_t *_algo_state);
int32_t TMF8801_FactoryCalib(TMF8801_Object_t *pObj, uint8_t *pCalibData);
int32_t TMF8801_ReadID(TMF8801_Object_t *pObj, uint8_t *Id);
int32_t TMF8801_WaitForCpuReady(TMF8801_Object_t *pObj);
int32_t TMF8801_LoadApplication(TMF8801_Object_t *pObj);
int32_t TMF8801_WaitForApplication(TMF8801_Object_t *pObj);
int32_t TMF8801_WaitForFactortCalib(TMF8801_Object_t *pObj);
uint16_t TMF8801_GetDistance_mm(TMF8801_Object_t *pObj);
#endif /* TMF8801_GENERIC_H_ */
